<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-01-14
 * Time: 13:50
 */
namespace smartybuilder\libs\bind;
include_once __DIR__.DIRECTORY_SEPARATOR.'../Smarty.class.php';
class Form
{
    protected $config = [

    ];
    protected $action = '';
    protected $forms = [];
    protected $button = [];
    protected $editorfileapi = '';
    protected $header = [
        'title'=>'',
        'keywords'=>'',
        'description'=>'',
    ];

    /**设置配置
     * @param array $array
     * @return $this
     */
    public function setConfig($array=[]){
        if(!empty($array)){
            if(is_array($array)){
                foreach ($array as $key=>$value){
                    $this->config[$key] = $value;
                }
            }
        }
        return $this;
    }

    /**获取配置
     * @param null $str
     * @return array|bool|mixed
     */
    public function getConfig($str=null){
        if(!empty($str)){
            if(isset($this->config[$str])){
                return $this->config[$str];
            }else{
                return false;
            }
        }
        return $this->config;
    }

    /**设置头部
     * @param array $header
     * @return $this
     */
    public function setHeader($title='',$keywords='',$description=''){
        $this->header = [
          'title'=>$title,
          'keywords'=>$keywords,
          'description'=>$description,
        ];
        return $this;
    }

    /**设置表单提交地址
     * @param string $action
     * @return $this
     */
    public function setAction($action=''){
        $this->action = $action;
        return $this;
    }

    /**设置form表单的数据
     * @param string表单类型 $type
     * @param string字段名字 $desc
     * @param string提交字段名称 $name
     * @param string单选，多选的数据 $option
     * @param string标注 $tagging
     * @param array选择框的值的字段和显示字段 $key_value
     * @param int是指option是几维数组，最多可填写2,为2时key_value必须填写  $radio_value
     * @param string联动单选，联动多选，文件，的上传地址, $action
     * @param string其余样式 $style
     * @return $this
     */
    public function set_up_form($type='input',$desc='未设置',$name='',$value='',$option='开启,关闭',$tagging='',$radio_value=1,$key_value=['value'=>'id','show'=>'name'],$action='',$style=''){
        if(!is_array($option)){
            $option = explode(',',$option);
        }
        $types = 'form/'.$type.'.html';
        if($type=='tree'){
            $option = json_encode($option);
        }
        if(is_array($value)){
            $value = json_encode($value);
        }
        $newdata = [
          'type'=>$types,
          'desc'=>$desc,
          'name'=>$name,
          'option'=>$option,
          'tagging'=>$tagging,
          'key_value'=>$key_value,
          'action'=>$action,
          'style'=>$style,
          'value'=>$value,
          'radio_value'=>$radio_value,
          'types'=>$type,
        ];
        $this->forms[] = $newdata;
        return $this;
    }

    /**设置编辑框上传文件的api接口
     * @param string $str
     * @return $this
//     */
//    public function setEditorFileApi($str=''){
//        $this->editorfileapi = $str;
//        return $this;
//    }

    /**设置按钮
     * @param string $type
     * @param string $desc
     * @param string $style
     * @return $this
     */
    public function set_button($type='button',$desc='提交',$style=''){
        $this->button[] = [
          'type'=>$type,
          'desc'=>$desc,
          'style'=>$style,
        ];
        return $this;
    }
    public function fetch(){
        $str = include_once __DIR__.'/../config.php';
        $smarty = new \Smarty();
        $smarty ->template_dir = __DIR__.DIRECTORY_SEPARATOR."../Templates";
        $smarty ->compile_dir  = __DIR__.DIRECTORY_SEPARATOR."../Templates_c";
        $smarty ->left_delimiter   = "{{";
        $smarty ->right_delimiter  = "}}";
        $smarty ->assign('action',$this->action);
        $smarty ->assign('button',$this->button);
        $smarty ->assign('form',$this->forms);
        $smarty->assign('path',$str['path']);
        $smarty->assign('header',$this->header);
        $smarty->assign('editorfileapi',$this->editorfileapi);
//        echo '<pre>';
//        print_r($this->forms);
//        exit;
        return $smarty->display('form.html');
    }

}